package com.example.easybuy.controller;

import com.example.easybuy.pojo.Page;
import com.example.easybuy.pojo.Productcategory;
import com.example.easybuy.pojo.ProductsManage;
import com.example.easybuy.service.ProductcategoryService;
import com.example.easybuy.service.ProductcategoryServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ProductcategoryController {
    private Logger logger= Logger.getLogger(ProductcategoryServiceImpl.class);

    @Autowired
    private ProductcategoryService productcategoryService;


    /**
     * 查询所有一级菜单的商品
     * @param
     * @return
     */
    @RequestMapping("proListOne")
    @ResponseBody
    @CrossOrigin
    public List<Productcategory> proListOne(String type){
        logger.debug("ProductcategoryController proListOne type: "+type);
        List<Productcategory> productcategoryList=productcategoryService.proListOne(type);
        logger.debug("ProductcategoryController proListOne type: "+type+" productcategoryList: "+productcategoryList);
        return productcategoryList;
    }


    /**
     * 查询所有菜单的商品
     * @param
     * @return
     */
    @RequestMapping("selectProClass")
    @ResponseBody
    @CrossOrigin
    public List<Productcategory> selectProClass(String type){
        logger.debug("ProductcategoryController selectProClass type: "+type);
        List<Productcategory> productcategoryList=productcategoryService.selectProClass(type);
        logger.debug("ProductcategoryController selectProClass type: "+type+" productcategoryList: "+productcategoryList);
        return productcategoryList;
    }




    /**
     * 查询所有商品分类
     */
    @RequestMapping("proListLeftView")
    @ResponseBody
    @CrossOrigin
    public List<Productcategory> proListLeftView(){
        logger.debug("ProductcategoryController proListLeftView ");
        List<Productcategory> productcategoryList=productcategoryService.proListView();
        logger.debug("ProductcategoryController proListLeftView productcategoryList :"+productcategoryList);
        return productcategoryList;
    }

    /*
    查询123级分类
     */
    @RequestMapping("getProductcategoryClass")
    @ResponseBody
    @CrossOrigin
    public List<Productcategory> getProductcategoryClass(String type){
        logger.debug("ProductcategoryController-getProductcategoryClass()- type:"+type);
        List<Productcategory> productcategoryList=productcategoryService.getProductcategoryClass(type);
        logger.debug("ProductcategoryController-getProductcategoryClass()-productcategoryList："+productcategoryList);
        return productcategoryList;
    }

    /*
    分页查询商品分类
     */
    @RequestMapping("getPageProducategory")
    @ResponseBody
    @CrossOrigin
    public Object getPageProducategory(Page page){
        logger.debug("ProductcategoryController-getProductcategoryClass()- page:"+page);
        List<Productcategory> productcategoryList=productcategoryService.getPageProducategory(page);
        page.setDataList(productcategoryList);
        logger.debug("ProductcategoryController-getProductcategoryClass()- productcategoryList:"+productcategoryList);
        return page;
    }

    /*
    删除分类
     */
    @RequestMapping("deletClassById")
    @ResponseBody
    @CrossOrigin
    public String deletClassById(String id){
        logger.debug("ProductcategoryController-deletClassById()- id:"+id);
        String success=productcategoryService.deletClassById(id);
        logger.debug("ProductcategoryController-deletClassById()- success:"+success);
        return success;
    }

    /*
    查询父级目录
     */
    @RequestMapping("getParent")
    @ResponseBody
    @CrossOrigin
    public List getParent(){
        logger.debug("ProductcategoryController-getParent()...start...");
        List proList=productcategoryService.getParent();
        logger.debug("ProductcategoryController-getParent()-proList："+proList);
        return proList;
    }

    /*
    添加分类商品
     */
    @RequestMapping("AddProClass")
    @ResponseBody
    @CrossOrigin
    public String AddProClass(int classifyone,int classifyParent,String namePro){
        logger.debug("ProductcategoryController-AddProClass()-classifyone："+classifyone
                +",classifyParent:"+classifyParent+",namePro:"+namePro);
        Productcategory productcategory=new Productcategory();
            productcategory.setType(classifyone);
        productcategory.setParentId(classifyParent);
        productcategory.setName(namePro);
        String success=productcategoryService.AddProClass(productcategory);
        logger.debug("ProductcategoryController-AddProClass()-success："+success);
            return success;
    }

    /*
    修改商品
     */
    @RequestMapping("updataPro")
    @ResponseBody
    @CrossOrigin
    public String updataPro(int classifyone,int classifyParent,String namePro,int id){
        logger.debug("ProductcategoryController-updataPro()-classifyone："+classifyone
                +",classifyParent:"+classifyParent+",namePro:"+namePro+",id:"+id);
        Productcategory productcategory=new Productcategory();
        //判断添加的级别
       productcategory.setType(classifyone);
        productcategory.setParentId(classifyParent);
        productcategory.setName(namePro);
        productcategory.setId(id);
        String success=productcategoryService.updataPro(productcategory);
        logger.debug("ProductcategoryController-updataPro()-success："+success);
        return success;
    }

    /*
    根据id分类查询
     */
    @RequestMapping("getProClassById")
    @ResponseBody
    @CrossOrigin
    public Object getProClassById(String id){
        logger.debug("ProductcategoryController-getProClassById()-id："+id);
        Productcategory productcategory=productcategoryService.getProClassById(id);
        logger.debug("ProductcategoryController-getProClassById()-productcategory："+productcategory);
        return productcategory;
    }

    /*
    删除分类的时候判断是否存在商品
     */
    @RequestMapping("getClassIsProducts")
    @ResponseBody
    @CrossOrigin
    public List<ProductsManage> getClassIsProducts(String id){
        logger.debug("ProductcategoryController-getClassIsProducts()-id："+id);
        List<ProductsManage> proList=productcategoryService.getClassIsProducts(id);
        logger.debug("ProductcategoryController-getClassIsProducts()-proList："+proList);
        return proList;
    }

}
